package net.runsystem.magiccloud;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * Man hinh Openning
 * 
 * @author Nui
 * 
 */

public class MainActivity extends Activity {

	private static String TAG = MainActivity.class.getName();

	/**
	 * Time for show splash ( in second )
	 */
	private static int SLEEP_TIME = 5;

	/**
	 * Indicate user touch on screen
	 * 
	 */
	private static boolean IS_TOUCH;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		// Remove title bar
		// this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.a01_layout);

		IS_TOUCH = false;

		// Start timer and launch main activity
		IntentLauncher launcher = new IntentLauncher();
		launcher.start();

		RelativeLayout splashLayout = (RelativeLayout) findViewById(R.id.a01_layout);

		splashLayout.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				IS_TOUCH = true;
				return false;
			}
		});
	}

	private class IntentLauncher extends Thread {

		@Override
		/**
		 * Sleep for some time and than start new activity.
		 */
		public void run() {
			try {
				for (int i = 0; i < SLEEP_TIME * 10; i++) {
					// Sleeping
					Log.e("Splash", "i: " + i + " SLEEP_TIME: " + SLEEP_TIME);
					Thread.sleep(100);
					if (IS_TOUCH) {
						i = SLEEP_TIME * 10;
					}
				}
			} catch (Exception e) {
				Log.e(TAG, e.getMessage());
			}
			Intent intent = new Intent(MainActivity.this, LoginActivity.class);
			MainActivity.this.startActivity(intent);
			MainActivity.this.finish();

		}
	}

}
